﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Navigation;
using Halfwit.Views;
using System.Windows.Controls;
using Halfwit.Properties;
using System.Deployment.Application;
using TweetSharp.Twitter.Extensions;
using TweetSharp.Twitter.Fluent;
using TweetSharp.Twitter.Model;

namespace Halfwit.ViewModels
{
    public class OptionsViewModel : INotifyPropertyChanged
    {
        public OptionsViewModel(MainViewModel host)
        {
            Title = "Options";

            _host = host;

            SampleTweet = new TwitterStatus
            {
                Id = -1,
                User = new TwitterUser
                {
                    Id = -1,
                    ProfileImageUrl = "http://s.twimg.com/a/1265049865/images/default_profile_3_normal.png",
                    ScreenName = "UserName",
                },
                Text = "This is a sample tweet. It will update as you change your display options.",
                CreatedDate = DateTime.Now,
            };

            FontScale = Properties.Settings.Default.FontScale;
            TweetWidth = Properties.Settings.Default.TweetWidth;
            RefreshInterval = Properties.Settings.Default.RefreshInterval;
            MentionsRefreshInterval = Properties.Settings.Default.MentionsRefreshInterval;
            MessagesRefreshInterval = Properties.Settings.Default.MessagesRefreshInterval;
            SearchPhrase = Properties.Settings.Default.HomeSearchPhrase;
            //FocusListItems = Properties.Settings.Default.FocusListItems;
            OpenLinksInBrowser = Properties.Settings.Default.OpenLinksInBrowser;
            MinimizeToTray = Properties.Settings.Default.MinimizeToTray;
            MaxTweets = Properties.Settings.Default.MaxTweets;

            this.Save = new RelayCommand("Save", SaveOptions);
            this.Cancel = new RelayCommand("Cancel", GoBack);
        }

        private MainViewModel _host;
        private string _status;
        private double _fontScale;
        private double _tweetWidth;
        private byte _refreshInterval;
        private byte _mentionsRefreshInterval;
        private byte _messagesRefreshInterval;
        private string _searchPhrase;
        //private bool _focusListItems;
        private bool _openLinksInBrowser;
        private bool _minimizeToTray;
        private int _maxTweets;

        private void SaveOptions(object o)
        {
            //var ns = NavigationService.GetNavigationService(Owner);
            //if (ns == null) return;

            Properties.Settings.Default.FontScale = FontScale;
            Properties.Settings.Default.TweetWidth = TweetWidth;
            Properties.Settings.Default.RefreshInterval = RefreshInterval;
            Properties.Settings.Default.MentionsRefreshInterval = MentionsRefreshInterval;
            Properties.Settings.Default.MessagesRefreshInterval = MessagesRefreshInterval;
            Properties.Settings.Default.HomeSearchPhrase = SearchPhrase;
            //Properties.Settings.Default.FocusListItems = FocusListItems;
            Properties.Settings.Default.OpenLinksInBrowser = OpenLinksInBrowser;
            Properties.Settings.Default.MinimizeToTray = MinimizeToTray;
            Properties.Settings.Default.MaxTweets = MaxTweets;
            Properties.Settings.Default.Save();

            _host.NavigationService.GoBack();
        }

        private void GoBack(object o)
        {
            if (!_host.NavigationService.CanGoBack)
            {
                App.Current.Shutdown();
                return;
            }
            _host.NavigationService.GoBack();
        }

        public string Title { get; private set; }
        //public DependencyObject Owner { get; set; }
        public TwitterStatus SampleTweet { get; private set; }
        public ICommand Save { get; private set; }
        public ICommand Cancel { get; private set; }

        public string Status
        {
            get { return _status; }
            set
            {
                _status = value;
                OnPropertyChanged("Status");
            }
        }

        public double FontScale
        {
            get { return _fontScale; }
            set
            {
                _fontScale = value;
                OnPropertyChanged("FontScale");
            }
        }

        public double TweetWidth
        {
            get { return _tweetWidth; }
            set
            {
                _tweetWidth = value;
                OnPropertyChanged("TweetWidth");
            }
        }

        public string SearchPhrase
        {
            get { return _searchPhrase; }
            set
            {
                _searchPhrase = value;
                OnPropertyChanged("SearchPhrase");
            }
        }

        public byte RefreshInterval
        {
            get { return _refreshInterval; }
            set
            {
                _refreshInterval = value;
                OnPropertyChanged("RefreshInterval");
            }
        }

        public byte MentionsRefreshInterval
        {
            get { return _mentionsRefreshInterval; }
            set
            {
                _mentionsRefreshInterval = value;
                OnPropertyChanged("MentionsRefreshInterval");
            }
        }

        public byte MessagesRefreshInterval
        {
            get { return _messagesRefreshInterval; }
            set
            {
                _messagesRefreshInterval = value;
                OnPropertyChanged("MessagesRefreshInterval");
            }
        }

        public bool OpenLinksInBrowser
        {
            get { return _openLinksInBrowser; }
            set
            {
                _openLinksInBrowser = value;
                OnPropertyChanged("OpenLinksInBrowser");
            }
        }

        public bool MinimizeToTray
        {
            get { return _minimizeToTray; }
            set
            {
                _minimizeToTray = value;
                OnPropertyChanged("MinimizeToTray");
            }
        }

        public int MaxTweets
        {
            get { return _maxTweets; }
            set 
            {
                _maxTweets = value;
                OnPropertyChanged("MaxTweets");
            }
        }

        //public bool FocusListItems
        //{
        //    get { return _focusListItems; }
        //    set
        //    {
        //        _focusListItems = value;
        //        OnPropertyChanged("FocusListItems");
        //    }
        //}

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
